import 'package:dsp_bt_audio/lang/my_i19n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';

import '../common/dialog.dart';

class DialogBluetooth extends DialogWidget {
  @override
  DialogBluetoothState createState() => DialogBluetoothState();
}

class DialogBluetoothState extends DialogWidgetState implements BLESearchTableViewDelegate {
  BLESearchTableView? _tableView;
  @override
  void initState() {
    super.initState();
    _tableView = BLESearchTableView(
      filterName: const ['NIO SUB'],
      agreement: this,
      contentColor: Colors.black,
      bgViewForHeader: (child) => Container(color: Colors.transparent, child: child),
      bgViewForFooter: (child) => Container(color: Colors.transparent, child: child),
    );
  }

  @override
  double get height => 400;

  @override
  Widget get child {
    return Container(color: Colors.white, child: _tableView);
  }

  @override
  List<RowModel> listModelForFooter(String appVersion) {
    return [];
  }

  @override
  List<RowModel> listModelForHeader(String appVersion) {
    return [];
  }

  @override
  List<RowModel> listModelForSection(List<String> list) {
    return [];
  }

  @override
  void methodForClickBLESearchTableView(BLESearchTableViewClickType type, String tag) {
    switch (type) {
      case BLESearchTableViewClickType.header:
        break;
      case BLESearchTableViewClickType.footer:
        break;
      case BLESearchTableViewClickType.section:
        break;
    }
  }

  @override
  Widget viewForRowInSection(DSPeripheral peripheral, int section, int row, bool isOn) {
    bool connect = peripheral.peripheral?.result.device.isConnected ?? false;
    return SizedBox(
      height: 30,
      child: Material(
        color: Colors.transparent,
        child: InkWell(
          onTap: () {
            connect
                ? BluetoothManager.instance.disconnect(peripheral.peripheral!)
                : BluetoothManager.instance.connect(peripheral.peripheral!);
          },
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(peripheral.peripheral?.result.device.advName ?? ''),
              Text(
                connect ? I19n.i.connect : I19n.i.disconnect,
                style: TextStyle(color: connect ? Colors.blue : Colors.red, fontSize: 14, fontWeight: FontWeight.bold),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
